package com.yjd.bi.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.bi.comm.dao.ICityDao;
import com.yjd.bi.comm.model.CityModel;
import com.yjd.bi.report.enums.StatisticsTypeEnum;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsMapModel;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.model.MapData;
import com.yjd.bi.report.model.MapSeries;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.user.model.UserAreaStatisticsModel;
import com.yjd.bi.user.model.UserSexStatisticsModel;
import com.yjd.bi.user.model.UserAgeStatisticsModel;
import com.yjd.bi.user.model.UserStatisticsConditionModel;
import com.yjd.bi.user.service.IUserStatisticsConditionService;
import com.yjd.bi.user.dao.IUserStatisticsConditionDao;

/**
 * <b>description</b>：每日汇总统计业务实现<br>
 * <b>time</b>：2016-04-26 16:48:56 <br>
 * <b>author</b>： carl
 */
@Service("userStatisticsConditionService")
public class UserStatisticsConditionServiceImpl extends BaseServiceImpl
		implements IUserStatisticsConditionService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public UserStatisticsConditionModel insert(
			UserStatisticsConditionModel model) throws Exception {
		return this.userStatisticsConditionDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(UserStatisticsConditionModel model) throws Exception {
		return this.userStatisticsConditionDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.userStatisticsConditionDao.deleteModel(paramMap, true);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public EChartsModel getDates(TimeType timeType, Integer sex, Long timeFrom,
			String field) throws Exception {
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList());
		ec.setLegend(FrameUtil.newArrayList(DateUtil.getFormate(timeFrom,
				DateUtil.PATTERN_yyyy_MM_dd)));
		ec.addSeries(FrameUtil.newArrayList(),
				DateUtil.getFormate(timeFrom, DateUtil.PATTERN_yyyy_MM_dd));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		if(timeType==TimeType.DAY){
		for (int i = searchParam.size() - 1; i >= 0; i--) {
			long date = searchParam.get(i).get(1);
			ec.getXAxis().add(
					DateUtil.getFormateStringByTimeDimention(date, "yyyy-MM-dd"));
			Double sum = ArithUtil.round(this.userStatisticsConditionDao
					.userSexSum(FrameUtil.newHashMap("field", field, "start",
							searchParam.get(i).get(0), "end", date,
							"condition", sex), false), 2);
			ec.getSeries().get(0).getData()
					.add(NumberUtil.formatNumber(sum, "##0.00"));
		}
		}else{
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
				// 注册用户数
				Integer sum=this.userStatisticsConditionDao.getSummary(FrameUtil.newHashMap( "start",DateUtil.dateStringToTimestamp(searchParam
						.get(i).get(0).toString(), DateUtil.PATTERN_yyyyMMdd), "end",DateUtil.dateStringToTimestamp( searchParam.get(i).get(1).toString(), DateUtil.PATTERN_yyyyMMdd),"propertyName", field,"condition",sex));
				ec.getSeries().get(0).getData()
				.add(NumberUtil.formatNumber(sum, NumberUtil.FORMAT_1));
			}
		}
		return ec;

	}
	
	
	

	@SuppressWarnings("rawtypes")
	@Override
	public UserSexStatisticsModel getSummaryModel(Map paramMap, boolean write)
			throws Exception {
		
		return this.userStatisticsConditionDao.getSummaryModel(paramMap, false);
	}



	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getUserNumByAge(StatisticsTypeEnum statisticsType)
			throws Exception {
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList());
		ec.setLegend(FrameUtil.newArrayList(statisticsType.getDesc()));
		ec.addSeries(FrameUtil.newArrayList(),statisticsType.getDesc());
		List<UserAgeStatisticsModel> ageList = null;
		
		if(StatisticsTypeEnum.invest.getType().equals(statisticsType.getType())){
			ageList = userStatisticsConditionDao.getInvestNumByAge(true);
		}else if(StatisticsTypeEnum.register.getType().equals(statisticsType.getType())){
			ageList = userStatisticsConditionDao.getRegisterNumByAge(true);
		}else if(StatisticsTypeEnum.recharge.getType().equals(statisticsType.getType())){
			ageList = userStatisticsConditionDao.getRechargeNumByAge(true);
		}else if(StatisticsTypeEnum.login.getType().equals(statisticsType.getType())){
			ageList = userStatisticsConditionDao.getLoginNumByAge(true);
		}
		
		if(ageList!=null&&ageList.size()>0){
			Map<Integer,Integer> statisticsMap = new HashMap<Integer,Integer>();
			int num1 = 0; //小于18岁
			int num2 = 0; //大于80岁
			
			for(UserAgeStatisticsModel model:ageList){
				int age = model.getAge();
				int num = model.getNum();
				
				if(age<18){
					num1 += num;	
				}
				
				if(age>80){
					num2 += num;	
				}
				
				statisticsMap.put(age, num);
			}
			List<String> data = ec.getSeries().get(0).getData();
			ec.getXAxis().add("18岁以下");
			data.add(NumberUtil.formatNumber(num1, NumberUtil.FORMAT_1));
			for(int i = 18 ;i<=80;i++){
				ec.getXAxis().add(i+"");
				data.add(statisticsMap.get(i)+"");
			}
			ec.getXAxis().add("80以上");
			data.add(NumberUtil.formatNumber(num2, NumberUtil.FORMAT_1));
		}

		return ec;
	}

     
    

	@Override
	public EChartsMapModel getUserNumByArea(StatisticsTypeEnum statisticsType)
			throws Exception {
		EChartsMapModel mapModel = new EChartsMapModel();
		mapModel.setLegend(FrameUtil.newArrayList(statisticsType.getDesc()));
		MapSeries mapSeries = new MapSeries();
		mapSeries.setName(statisticsType.getDesc());
		
		List<MapData> dataList = new ArrayList<MapData>();
		
		List<UserAreaStatisticsModel> areaList = null;
		if(StatisticsTypeEnum.invest.getType().equals(statisticsType.getType())){
			areaList = userStatisticsConditionDao.getInvestNumByArea(true);
		}else if(StatisticsTypeEnum.register.getType().equals(statisticsType.getType())){
			areaList = userStatisticsConditionDao.getRegisterNumByArea(true);
		}else if(StatisticsTypeEnum.recharge.getType().equals(statisticsType.getType())){
			areaList = userStatisticsConditionDao.getRechargeNumByArea(true);
		}
		
		if(areaList!=null&&areaList.size()>0){
		
			Map<Long, Integer> areaMap = new HashMap<Long, Integer>();
			for(UserAreaStatisticsModel model :areaList){
				areaMap.put(model.getCity_id(), model.getNum());
			}
			
			Map<Object,Object> param = new HashMap<Object,Object>();
			param.put("parentid", 0);
			param.put("sort", "id desc");
			List<CityModel> cityList = cityDao.getModelList(param,false);
			
			if(cityList.size()>0){
				for(CityModel city :cityList ){
					MapData mapData = new MapData();
					mapData.setName(city.getName());
					mapData.setValue(areaMap.get(city.getId())==null?0:areaMap.get(city.getId()));
					dataList.add(mapData);
				}
			}
			
		}
		mapSeries.setDataList(dataList);
		mapModel.setMapSeries(mapSeries);
		return mapModel;
	}

	@Override
	public Integer getSummary(Map<String, Object> paramMap)
			throws Exception {
		return userStatisticsConditionDao.getSummary(paramMap);
	}


	@Resource
	private IUserStatisticsConditionDao userStatisticsConditionDao;

	@Resource
	private ICityDao cityDao;
	
	@Override
	public IBaseDao getBaseDao() {
		return this.userStatisticsConditionDao;
	}

}
